package com.okcl.manager.controller;

import com.github.pagehelper.PageInfo;
import com.okcl.manager.service.SysUserService;
import com.okcl.model.dto.system.AssginRoleDto;
import com.okcl.model.dto.system.SysUserDto;
import com.okcl.model.entity.system.SysUser;
import com.okcl.model.vo.common.Result;
import com.okcl.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "系统用户接口")
@RequestMapping("/admin/system/sysUser")
@RequiredArgsConstructor
public class SysUserController {
    private final SysUserService sysUserService;

    /**
     * 用户列表查询
     *
     * @param sysUserDto
     * @return
     */
    @Operation(summary = "用户列表查询")
    @PostMapping("/userList")
    public Result queryPageUserList(@RequestBody SysUserDto sysUserDto) {
        PageInfo<SysUser> pageInfo = sysUserService.queryPageUserList(sysUserDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 添加用户
     *
     * @param sysUser
     * @return
     */
    @Operation(summary = "添加用户")
    @PostMapping("/add")
    public Result save(@RequestBody SysUser sysUser) {
        sysUserService.save(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改用户
     *
     * @param sysUser
     * @return
     */
    @Operation(summary = "修改用户")
    @PutMapping("/edit")
    public Result edit(@RequestBody SysUser sysUser) {
        sysUserService.edit(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除用户")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id") Long id) {
        sysUserService.delete(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 为用户分配角色
     *
     * @param assginRoleDto
     * @return
     */
    @Operation(summary = "分配角色")
    @PostMapping("/assignRole")
    public Result assignRole(@RequestBody AssginRoleDto assginRoleDto) {
        sysUserService.assignRole(assginRoleDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据用户id查询用户绑定角色id列表
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询用户绑定角色id")
    @GetMapping("/roleIdList/{id}")
    public Result queryRoleIdList(@PathVariable(value = "id") Long id) {
        List<Long> roleIdList = sysUserService.getRoleIdList(id);
        return Result.build(roleIdList, ResultCodeEnum.SUCCESS);
    }
}
